package businessLogic.facade;

import businessLogic.service.IService;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import vo.IValueObject;

/**
 *
 * @author Alexander
 */
public abstract class Facade {

    private IService service;

    public void create(IValueObject vo) {
        EntityManagerFactory emf = null;
        EntityManager em = null;
        EntityTransaction tx = null;
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            service.persist(vo, em);
            tx.commit();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            if (em != null && tx != null) {
                tx.rollback();
            }
        } finally {
            if (em != null) {
                em.clear();
                em.close();
            }
        }
    }

    public IValueObject find(Object id) {
        EntityManagerFactory emf = null;
        EntityManager em = null;
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            return service.find(id, em);
        } finally {
            if (em != null) {
                em.clear();
                em.close();
            }
        }

    }

    public boolean update(IValueObject vo) {
        EntityManagerFactory emf = null;
        EntityManager em = null;
        EntityTransaction tx = null;
        boolean ret = false;
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            service.update(vo, em);
            tx.commit();
            ret = true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            if (!exist(vo)) {
                throw new Exception("La entidad a actualizar no existe");
            }
            if (em != null && tx != null) {
                tx.rollback();
            }
        } finally {
            if (em != null) {
                em.clear();
                em.close();
            }
            return ret;
        }
    }
    
    public boolean delete (Object id){
        EntityManagerFactory emf = null;
        EntityManager em = null;
        EntityTransaction tx = null;
        boolean ret = false;
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            service.delete(id, em);
            tx.commit();
            ret = true;
        }catch (Exception e) {
            System.out.println(e.getMessage());
            if (em != null && tx != null) {
                tx.rollback();
            }
        } finally {
            if (em != null) {
                em.clear();
                em.close();
            }
            return ret;
        }
    }
    
    public List<IValueObject> getList (){
         EntityManagerFactory emf = null;
        EntityManager em = null;
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            return service.getList(em);
        } finally {
            if (em != null) {
                em.clear();
                em.close();
            }
        }
    }

    public IService getService() {
        return service;
    }

    public void setService(IService service) {
        this.service = service;
    }

    protected abstract boolean exist(IValueObject vo);
}
